package com.capacitydorm.server.service;

import com.capacitydorm.server.common.vo.*;
import com.capacitydorm.server.dao.*;
import com.capacitydorm.server.domain.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional

public class WxuserService {

    @Resource
    private WxuserMapper wxuserMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private DormMapper dormMapper;

    @Resource
    private FloorMapper floorMapper;

    @Resource
    private TowerMapper towerMapper;

    @Resource
    private  StudentMapper studentMapper;

    /**
     *
     * @param user
     * @return
     */
    public List<User> login(User user) {
        List<User> users = userMapper.select(user);
        return users;
    }

    public void updateQuickSeach(wxuser wxuser) {
        wxuser wxuser1 = wxuserMapper.selectByPrimaryKey(wxuser);
        wxuser1.setQuickSeach(wxuser.getQuickSeach());
        wxuserMapper.updateByPrimaryKey(wxuser1);
    }

    public List<QuickCheckVo> selectQuickSeach(Integer id) {
        List<String> list = dormMapper.selectAllNum();
        Collections.sort(list);
        wxuser wxuser = wxuserMapper.selectByPrimaryKey(id);
        String quickSeach = wxuser.getQuickSeach();
        String[] split = quickSeach.split( ",");
        List<String> strings = Arrays.asList(split);
        List<QuickCheckVo> quickCheckVoList = new ArrayList<>();
        for (String number : list) {
            QuickCheckVo q = new QuickCheckVo();
            q.setDromNum(number);
            for (String quickCheck : strings) {
                if (number.equals(quickCheck)) {
                    q.setQuickCheck(true);
                }
            }
            quickCheckVoList.add(q);
        }
        return quickCheckVoList;
    }

    public List<Dorm> quickSeach(Integer id) {
        List<Dorm> dorms = new ArrayList<>();
        wxuser wxuser = wxuserMapper.selectByPrimaryKey(id);
        if (wxuser == null){
            return dorms;
        }
        String quickSeach = wxuser.getQuickSeach();
        String[] split = quickSeach.split(",");
        if(split !=null) {
            for (String s : split) {
                Dorm byNum = dormMapper.findByNum(s);
                if (byNum!=null) {
                    List<Student> byDId = studentMapper.findByDId(byNum.getId());
                    byNum.setStudents(byDId);
                    dorms.add(byNum);
                }
            }
        }
        return dorms;
    }

    public ResultVo selectAll(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<wxuser> wxusers = wxuserMapper.selectAll();
        PageInfo<wxuser> info = new PageInfo<>(wxusers);
        ResultVo resultVo = new ResultVo();
        resultVo.setRows(info.getList());
        resultVo.setTotal(info.getTotal());
        return resultVo;
    }

    public void addUser(WxuserVo wxuserVo) {
        wxuser wxuser = new wxuser();
        wxuser.setOpenId(wxuserVo.getOpenId());
        wxuser.setName(wxuserVo.getName());
        wxuser.setClasses(wxuserVo.getClasses());
        wxuser.setPhone(wxuserVo.getPhone());
        wxuser.setSex(wxuserVo.getSex());

        wxuserMapper.insert(wxuser);
        wxuser wxuser1 = wxuserMapper.selectOne(wxuser);
        User user = new User();
        user.setPassword(wxuserVo.getPassword());
        user.setUsername(wxuserVo.getUsername());
        user.setWxid(wxuser1.getId());

        userMapper.insert(user);


    }

    public void delectWxuserById(Integer id) {
        wxuserMapper.deleteByPrimaryKey(id);
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("wxid", id);
        userMapper.deleteByExample(example);
    }

    public void updateWxuserById(WxuserVo wxuserVo) {
        wxuser wxuser = new wxuser();
        wxuser.setId(wxuserVo.getId());
        wxuser.setOpenId(wxuserVo.getOpenId());
        wxuser.setName(wxuserVo.getName());
        wxuser.setClasses(wxuserVo.getClasses());
        wxuser.setPhone(wxuserVo.getPhone());
        wxuser.setSex(wxuserVo.getSex());
        wxuser.setId(wxuserVo.getId());
        wxuserMapper.updateByPrimaryKey(wxuser);

        User user = new User();
        user.setPassword(wxuserVo.getPassword());
        user.setUsername(wxuserVo.getUsername());
        user.setWxid(wxuserVo.getId());

        userMapper.update(user);

    }

    public wxuser checkWxuserById(Integer id) {
        wxuser wxuser = wxuserMapper.selectByPrimaryKey(id);
        return wxuser;
    }


    public NewDormVo quickSeachAll(Integer id){
        wxuser wxuser = wxuserMapper.selectByPrimaryKey(id);
        String quickSeach = wxuser.getQuickSeach();

        NewDormVo newDormVo = new NewDormVo();
        List<TowerVo> towerVos = new ArrayList<>();

        List<Tower> towers = towerMapper.selectAll();
        for (Tower t: towers) {
            TowerVo towerVo = new TowerVo();
            towerVo.setId(t.getId());
            towerVo.setName(t.getName());
            List<FloorVo> floorVos = new ArrayList<>();
            List<Floor> byTowerId = floorMapper.findByTowerId(t.getId());
            for (Floor f: byTowerId) {
                FloorVo floorVo = new FloorVo();
                floorVo.setName(f.getNumber());
                floorVo.setId(f.getId()+"");
                floorVo.setHouse(t.getName());
                List<NumVo> dorms = new ArrayList<>();

                Example example = new Example(Dorm.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("floorId",f.getId());
                List<Dorm> list = dormMapper.selectByExample(example);
                for (Dorm d: list){
                    NumVo numVo = new NumVo();
                    numVo.setId(d.getId()+"");
                    numVo.setName(d.getNumber());
                    numVo.setFloor(f.getNumber());
                    if (quickSeach != null){
                        if (quickSeach.contains(d.getNumber())){
                            numVo.setQuickCheck(true);
                        }
                    }
                dorms.add(numVo);
                }
                floorVo.setDroms(dorms);
                floorVos.add(floorVo);
            }
            towerVo.setFloorList(floorVos);
            towerVos.add(towerVo);
        }
        newDormVo.setTower(towerVos);
        return newDormVo;
    }

    public User selectUser(Integer id) {
        User user = new User();
        user.setWxid(id);
        User user1 = userMapper.selectOne(user);
        return user1;
    }

    public List<wxuser> select() {
        return wxuserMapper.selectAll();
    }
}
